package cn.sh.yhk.plugin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;

/**
 * Hello world!
 *
 */
@Activate(group = Constants.PROVIDER, order = 999)
public class AdminYhkFilter implements Filter {

	Logger logger = Logger.getLogger(AdminYhkFilter.class);

	@Override
	public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
		
		
		System.out.println("——————————————————————————进入过滤器————————————————————————————————————");
		cn.sh.yhk.plugin.pojo.Logger mylog = new cn.sh.yhk.plugin.pojo.Logger();
		// 路径 方法
		Map<String, String> hashMap = invocation.getAttachments();
		mylog.setPath(hashMap.get("path"));
		mylog.setMethod(invocation.getMethodName());
		// 入参
		Object[] objs = invocation.getArguments();
		List<Object> incomes = new ArrayList<Object>();
		for (int i = 0; i < objs.length; i++) {
			incomes.add(objs[i]);
		}
		mylog.setIncome(incomes);
		// 调用
		Result result = invoker.invoke(invocation);
		if (result.getException() != null) {
			mylog.setThrowable(result.getException());
			logger.error(mylog);
		} else {
			mylog.setOutcome(result.getResult());
			logger.debug(mylog);
		}
		return result;
	}
}
